+ function($) {
    'use strict';

    var dismiss = '[data-dismiss="cart"]',
        $cartion = $('.carticon'),
        $carttotal = $('.carttotal i'),
        $cartbody = $('.cartbody')
    var Cart = function(element) {
        this.element = $(element)
    }
    console.log($('.carticon'));

    if($cartion.length == 0){
      $cartion = $('[data-dismiss="cartpopup"]')
    }



    Cart.prototype.init = function() {
        this.ruleid = this.element.closest('dd').data('ruleid') ? this.element.closest('dd').data('ruleid') : this.element.closest('li').attr('data-boxruleid')
        this.goodid = this.element.closest('dd').data('goodid') ? this.element.closest('dd').data('goodid') : this.element.closest('li').attr('data-boxgoodid')
        this.cartid = this.element.attr('data-cartid')
        this.$onenum = this.element.parent().find('.entityquantity')
        this.$minusoneEle = this.element.parent().find('[data-methods="minusone"]')
        this.$goodid = $('dd[data-goodid="' + this.goodid + '"]')
        var $this = this.element,
            methods = $this.data('methods');

        methods == 'addone' ? this.addone($this) : '';
        methods == 'minusone' ? this.minusone($this) : '';
        methods == 'addbox' ? this.addbox($this) : '';
        methods == 'minusbox' ? this.minusbox($this) : '';
        methods == 'addmore' ? this.addmore($this) : '';
    }

    Cart.prototype.addone = function() {
        var ts = this;
        $.post(addUrl, {
            "id": ts.ruleid
        }, function(data) {
            if (data.num) {
                ts.$onenum.text(data.num);
                ts.$minusoneEle.show().attr('data-cartid', data.id);
                $cartion.attr('attr-quantity', data.tnum).removeClass('empty');
                
                $cartion.find('.van-icon__info').text(data.tnum)
                $carttotal.text(data.total);

                $('.tgou .submitbutton').replaceWith(data.delivery_text)
              }else{
                mui.toast(data.msg)
              }
        }, "json");
    }

    Cart.prototype.addbox = function() {
        var ts = this;
        $.post(addUrl, {
            "id": ts.ruleid
        }, function(data) {
            if (data.num) {
                ts.$onenum.text(data.one_num);
                ts.$onenum.parents('.entitycartbutton').prev().text(data.one_price_total);
                ts.$goodid.find('.entityquantity').text(data.num)
                $cartion.attr('attr-quantity', data.tnum).removeClass('empty');
                $cartion.find('.van-icon__info').text(data.tnum)
                $carttotal.text(data.total);
                  $('.tgou .submitbutton').replaceWith(data.delivery_text)
              }else{
                mui.toast(data.msg)
              }
        }, "json");
    }

    Cart.prototype.addmore = function() {
        var ts = this,
            $this = this.element,
            ruleid = $this.attr('data-ruleid');
        $.post(addUrl, {
            "id": ruleid
        }, function(data) {
            if (data.num) {
                $this.closest('dd').find('.entityquantity').text(data.num);
                $this.closest('dd').find('[data-methods="minusone"]').show().attr('disabled', 'disabled');
                $cartion.attr('attr-quantity', data.tnum).removeClass('empty');
                $cartion.find('.van-icon__info').text(data.tnum)
                $carttotal.text(data.total);

                $this.parents('.mask').hide();
                $('.mui-col-xs-9').css('overflow-y','auto')

            }else{
              mui.toast(data.msg)
            }
            $('.tgou .submitbutton').replaceWith(data.delivery_text)
        }, "json");
    }

    Cart.prototype.minusone = function() {
        var ts = this
        if (this.element.attr('disabled') != "disabled") {
            $.post(minusUrl, {
                "id": ts.cartid
            }, function(data) {
                if (data.tnum > 0) {
                    if (data.one_num > 0) {
                        ts.$onenum.text(data.one_num)
                    } else {
                        ts.$onenum.text(" ")
                        ts.$minusoneEle.hide()
                    }
                    $cartion.attr('attr-quantity', data.tnum).removeClass('empty');
                    $cartion.find('.van-icon__info').text(data.tnum)
                    $carttotal.text(data.total);

                } else {
                    ts.$onenum.text(" ")
                    ts.$minusoneEle.hide()
                    $cartion.attr('attr-quantity', 0).addClass('empty');
                    $cartion.find('.van-icon__info').text(0)
                    $carttotal.text(0);

                }
                $('.tgou .submitbutton').replaceWith(data.delivery_text)
            }, "json");
        } else {
            mui.toast('多规格商品只能去购物车删除');
        }
    }

    Cart.prototype.minusbox = function() {
        var ts = this
        $.post(minusUrl, {
            "id": ts.cartid
        }, function(data) {
            if (data.tnum > 0) {
                if (data.one_num == 0) {
                    ts.$onenum.parents('.entityrow').remove();
                } else {
                    ts.$onenum.text(data.one_num)
                    ts.$onenum.parents('.entitycartbutton').prev().text(data.one_price_total);
                }
                if (data.num > 0) {
                    ts.$goodid.find('.entityquantity').text(data.num)

                } else if (data.num == 0) {
                    ts.$goodid.find('.entityquantity').text(" ")
                    ts.$goodid.find('[data-methods="minusone"]').hide();
                }
                $cartion.attr('attr-quantity', data.tnum);
                $cartion.find('.van-icon__info').text(data.tnum)
                $carttotal.text(data.total);

            } else {
                $cartbody.hide();
                $cartbody.prev().hide()
                ts.$goodid.find('.entityquantity').text(" ")
                ts.$goodid.find('[data-methods="minusone"]').hide();

                $cartion.attr('attr-quantity', data.tnum).addClass('empty');
                $cartion.find('.van-icon__info').text(data.tnum)
                $carttotal.text(data.total);
            }
              $('.tgou .submitbutton').replaceWith(data.delivery_text)
        }, "json");
    }
    Cart.prototype.showcartbox = function() {
        if ($cartbody.is(':hidden')) {
            $.post(cartListUrl, function(data) {
                var html = template('tpl', data);
                $('.cartlist').html(html);
                $cartbody.show();
                $cartbody.prev().show()
            }, "json");
        } else {
            $cartbody.hide();
            $cartbody.prev().hide()
        }
    }
    Cart.prototype.remove = function() {
        $.post(clearUrl, function(data) {
            $cartbody.hide();
            $cartbody.prev().hide()
            $('.entityquantity').text(" ");
            $('[data-methods="minusone"]').hide();
            $cartion.attr('attr-quantity', data.tnum).addClass('empty');
            $cartion.find('.van-icon__info').text(data.tnum)
            $carttotal.text(data.total);
            mui.toast('清空购物车成功！');
        }, "json");
    }

    function Plugin(option) {
        return this.each(function() {
            var $this = $(this)
            var data = $this.data('bs.cart')

            if (!data) $this.data('bs.cart', (data = new Cart(this)))
            if (typeof option == 'string') data[option]()
        })
    }

    var old = $.fn.cart

    $.fn.cart = Plugin
    $.fn.cart.Constructor = Cart

    $.fn.cart.noConflict = function() {
        $.fn.cart = old
        return this
    }


    var clickHandler = function(e) {
        e.preventDefault()
        Plugin.call($(this), 'init')
    }

    $(document)
        .on('click.bs.cart.data-api', dismiss, clickHandler)
    $cartion.on('click', function() {
        Cart.prototype.showcartbox()
    })
    $(".cartheader-remove").on('click', function() {
        Cart.prototype.remove()
        $cartion.find('.van-icon__info').text(0)
    })
    $(".cartmask").click(function() {
        $cartbody.hide();
        $cartbody.prev().hide()
    })

    $('.specadd').click(function() {
        var $this = $(this),
            $mask = $this.parents('.cartbutton').prev() ;
            if($mask.length ==0){
              $mask = $('.mask')
            }

        $('.mui-col-xs-9').css('overflow-y','inherit')
        $mask.show();
    })

    $('.close').click(function() {
        var $this = $(this),
            $mask = $this.parents('.mask');
        $('.mui-col-xs-9').css('overflow-y','auto')
        $mask.hide();
    })

    $(".candidator").click(function() {
        var $this = $(this),
            $selectedresult = $this.parent().next(),
            $cartadd = $this.parents('.candidators').find('.cartadd'),
            info = $this.attr('data-info').split('|'),
            ruleid = $this.attr('data-ruleid');
        if (!$this.hasClass('selected')) {
            $this.addClass('selected').siblings().removeClass('selected');
            var html = "<p class='selectedstr'>已选：" + info[0] + " <span class='stock'>库存：" + info[2] + "</span> <span class='weight'>重量：" + info[3] + "</span></p><p class='price'><span class='yen'>¥</span> <span class='now'>" + info[1] + "</span> </p>"
            $selectedresult.html(html);
            $cartadd.attr('data-ruleid', ruleid);
        }
    })

}(jQuery);
